﻿using StarryEdge.Core.Device;
using StarryEdge.Core.Parameter;
using StarryEdge.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StarryEdge.Core.Models;
using StarryEdge.Core.Models.Interact.Data;

namespace StarryEdge.Module.Handle.Handle
{
    public abstract class HandleBase
    {
        protected IStarryEdgeLog _log;
        protected IDeviceFactory _deviceFactory;
        protected ITemporaryParameter _temporary;
        protected ISystemConfigParameter _systemConfigParameter;
        protected IDialogHost _dialogHost;
        public HandleBase(IStarryEdgeLog starryEdgeLog, 
            IDialogHost dialogHost, 
            ITemporaryParameter temporaryParameter, 
            ISystemConfigParameter systemConfigParameter, 
            IDeviceFactory deviceFactory)
        {
            _log = starryEdgeLog;
            _deviceFactory = deviceFactory;
            _systemConfigParameter = systemConfigParameter;
            _temporary = temporaryParameter;
            _dialogHost = dialogHost;
        }

        protected void RunLog<T>(string message, Core.Enums.LogLevel logLevel, HandleContext<T> context)
        {
            _log.LogRun($"[{context.ServiceName}]-[{context.TaskName}]{message}", logLevel);
        }

        
        /// <summary>.
        /// 写值
        /// </summary>
        /// <param name="value">值</param>
        /// <param name="devices">PLC链接</param>
        protected void WriteCMD<T>(object value, T label, IPLC<T> devices, HandleContext<T> context)
        {
            for (int i = 0; i < 3; i++)
            {
                if (devices.WriteSingle(label, value, 0))
                {
                    RunLog($"-[{label}] 第[{i}]次 值 [{value}] 写入成功", Core.Enums.LogLevel.Success, context);
                    return;
                }
                else
                {
                    RunLog($"-[{label}] 第[{i}]次 值 [{value}] 写入失败", Core.Enums.LogLevel.Warning, context);
                }
            }
        }

    }
}
